package com.ky.gp.app.config;

import com.ky.common.config.SystemConfig;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @version 1.0
 * @Author 小胤
 * @Date 2024/6/7 14:00
 * @注释
 */
public class OpenFeignConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        //1.获取请求对象
        // RequestContextHolder 作用和 ThreadLocal类似，就是获取当前线程的副本request
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=attributes.getRequest();

        //2.把需要传递的请求消息头传输给OpenFeign
        // RequestTemplate 就是 feign的核心实现
        // 此时完成 将请求header中的token 配置到 RequestTemplate
        template.header(SystemConfig.HEADER_TOKEN,request.getHeader(SystemConfig.HEADER_TOKEN));


    }
}
